<template>
    <div id="psd">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="原密码" prop="oldpass">
                <el-input type="password" v-model="ruleForm.oldpass" autocomplete="off" @blur="checkPsd"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm()">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    data() {
        var validateOldpass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入原密码'));
            }
            setTimeout(() => {
                if (this.isSame == 'false') {
                    callback(new Error('原密码错误'));
                } else if (this.isSame == 'true') {
                    return;
                }
            }, 1000);
            // else if (this.isSame == 'true') {
            //     return;
            // } 


        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入新密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入新密码'));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                return;
            }
        };
        return {
            isSame: '',
            ruleForm: {
                oldpass: '',
                pass: '',
                checkPass: '',

            },
            rules: {
                oldpass: [
                    { validator: validateOldpass, trigger: 'blur' }
                ],
                pass: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
            }
        };
    },
    created() {
        // let jwt = localStorage.getItem("jwt");
        // this.$axios({
        //     method: 'post',
        //     url: "http://localhost:8080/api/v1/propertyStaff/getPsd",
        //     data: jwt,
        // }).then((r) => {
        //     console.log(r);
        //     if (r.data.code == 200) {

        //     }
        // })
    },
    methods: {
        submitForm() {
            let jwt = localStorage.getItem("jwt");
            var psdVo = {
                jwt,
                psd: this.ruleForm.pass,
            }

            if (this.isSame == "true" && this.ruleForm.pass == this.ruleForm.checkPass && this.ruleForm.pass != '') {
                this.$axios({
                    method: 'post',
                    url: "http://localhost:8080/api/v1/propertyStaff/updatePsd",
                    data: psdVo,
                }).then((r) => {
                    console.log(r);
                    if (r.data.code == 200) {
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                    }
                })

            } else {
                this.$message({
                    message: '信息不正确',
                    type: 'error'
                });
            }
            ;
        },
        resetForm(formName) {
            this.$message({
                message: '重置成功',
                type: 'success'
            });
            this.$refs[formName].resetFields();
        },
        checkPsd() {
            let jwt = localStorage.getItem("jwt");
            var psdVo = {
                jwt,
                psd: this.ruleForm.oldpass,
            }

            this.$axios({
                method: 'post',
                url: "http://localhost:8080/api/v1/propertyStaff/isSame",
                data: psdVo,
            }).then((r) => {
                console.log(r);
                if (r.data.code == 200) {
                    var msg = r.data.data;
                    if (msg == "nosame") {
                        this.isSame = "false";
                    } if (msg == "same") {
                        this.isSame = "true";
                    }

                }
            })
        }
    }
}
</script>
<style>
#psd {
    position: relative;
    left: 200px;
    top: 100px;
    width: 500px;
    height: 400px;
}
</style>